import datetime
import sys
import time

sys.path.append('..')

from dataloader.coins_information import update_all_coins_and_tags_information
from dataloader.ohlcvm import (update_daily_ohlcvm_all_coins,
                               update_ohlc_btc_and_eth)
from dataloader.deribit_option_data import update_deribit_option_trade_data
from dataloader.trade_amount import (get_exchanges_detail,
                                     update_total_marketcap_and_amount,
                                     update_binance_btc_to_stablecoin_amount,
                                     update_cex_spot_amount,
                                     update_cex_deri_amount,
                                     update_dex_amount)
from dataloader.usdt_premium import (update_usdcnh,
                                     update_usdtusd_premium_okex,
                                     update_usdtcny_premium_btc126)
from dataloader.funding_rate import update_all_binance_funding_rate
from dataloader.exchange_information import (update_perpetual_leverage_and_margin_binance,
                                             update_margin_interest_rate_binance,
                                             update_margin_interest_rate_okex,
                                             update_margin_interest_rate_aave)
from dataloader.crypto_social_data import (update_social_data_aicoin_bulletin,
                                           update_social_metrics_btc)
from dataloader.glassnode import update_all_indicators
from tools.logger import logger
import os
import traceback
from tools.task_monitor import task_to_db, send_error_to_email


if __name__ == '__main__':
    logger.info("开始daily run")
    weekday = datetime.datetime.weekday(datetime.datetime.now())
    t1 = time.time()
    # 更新币的信息和板块信息
    task_to_db(os.path.basename(__file__), 'update_all_coins_and_tags_information')
    try:
        update_all_coins_and_tags_information()
        task_to_db(os.path.basename(__file__), 'update_all_coins_and_tags_information', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_all_coins_and_tags_information", message=msg)
        # raise e
    t2 = time.time()
    # 更新行情数据
    task_to_db(os.path.basename(__file__), 'update_ohlc_btc_and_eth')
    try:
        update_ohlc_btc_and_eth()
        task_to_db(os.path.basename(__file__), 'update_ohlc_btc_and_eth', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_ohlc_btc_and_eth", message=msg)
        # raise e
    # if weekday == 2:
    #     update_daily_ohlcvm_all_coins()
    t3 = time.time()
    # 更新deribit期权交易数据
    task_to_db(os.path.basename(__file__), 'update_deribit_option_trade_data')
    try:
        update_deribit_option_trade_data()
        task_to_db(os.path.basename(__file__), 'update_deribit_option_trade_data', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_deribit_option_trade_data", message=msg)
        # raise e
    t4 = time.time()
    # 更新交易所信息
    # if weekday == 2:
    #     get_exchanges_detail()
    t5 = time.time()
    # 更新各类交易所交易量信息
    task_to_db(os.path.basename(__file__), 'update_total_marketcap_and_amount')
    try:
        update_total_marketcap_and_amount()
        task_to_db(os.path.basename(__file__), 'update_total_marketcap_and_amount', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_total_marketcap_and_amount", message=msg)
        # raise e

    task_to_db(os.path.basename(__file__), 'update_binance_btc_to_stablecoin_amount')
    try:
        update_binance_btc_to_stablecoin_amount()
        task_to_db(os.path.basename(__file__), 'update_binance_btc_to_stablecoin_amount', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_binance_btc_to_stablecoin_amount", message=msg)
        # raise e

    task_to_db(os.path.basename(__file__), 'update_cex_spot_amount')
    try:
        update_cex_spot_amount()
        task_to_db(os.path.basename(__file__), 'update_cex_spot_amount', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_cex_spot_amount", message=msg)
        # raise e

    task_to_db(os.path.basename(__file__), 'update_cex_deri_amount')
    try:
        update_cex_deri_amount()
        task_to_db(os.path.basename(__file__), 'update_cex_deri_amount', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_cex_deri_amount", message=msg)
        # raise e

    task_to_db(os.path.basename(__file__), 'update_dex_amount')
    try:
        update_dex_amount()
        task_to_db(os.path.basename(__file__), 'update_dex_amount', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_dex_amount", message=msg)
        # raise e
    t6 = time.time()

    # 更新USDT兑人民币相关
    task_to_db(os.path.basename(__file__), 'update_usdcnh')
    try:
        update_usdcnh()
        task_to_db(os.path.basename(__file__), 'update_usdcnh', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_usdcnh", message=msg)
        #raise e

    task_to_db(os.path.basename(__file__), 'update_usdtusd_premium_okex')
    try:
        update_usdtusd_premium_okex()
        task_to_db(os.path.basename(__file__), 'update_usdtusd_premium_okex', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_usdtusd_premium_okex", message=msg)
        #raise e

    task_to_db(os.path.basename(__file__), 'update_usdtcny_premium_btc126')
    try:
        update_usdtcny_premium_btc126()
        task_to_db(os.path.basename(__file__), 'update_usdtcny_premium_btc126', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_usdtcny_premium_btc126", message=msg)
        #raise e

    t7 = time.time()
    # 更新币安资金费率
    task_to_db(os.path.basename(__file__), 'update_all_binance_funding_rate')
    try:
        update_all_binance_funding_rate()
        task_to_db(os.path.basename(__file__), 'update_all_binance_funding_rate', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_all_binance_funding_rate", message=msg)
        #raise e

    t8 = time.time()
    # 更新交易所借贷利率、保证金杠杆比例等
    task_to_db(os.path.basename(__file__), 'update_perpetual_leverage_and_margin_binance')
    try:
        update_perpetual_leverage_and_margin_binance()
        task_to_db(os.path.basename(__file__), 'update_perpetual_leverage_and_margin_binance', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_perpetual_leverage_and_margin_binance", message=msg)
        #raise e

    task_to_db(os.path.basename(__file__), 'update_margin_interest_rate_binance')
    try:
        update_margin_interest_rate_binance()
        task_to_db(os.path.basename(__file__), 'update_margin_interest_rate_binance', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_margin_interest_rate_binance", message=msg)
        #raise e

    task_to_db(os.path.basename(__file__), 'update_margin_interest_rate_okex')
    try:
        update_margin_interest_rate_okex()
        task_to_db(os.path.basename(__file__), 'update_margin_interest_rate_okex', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_margin_interest_rate_okex", message=msg)
        #raise e

    task_to_db(os.path.basename(__file__), 'update_margin_interest_rate_aave')
    try:
        update_margin_interest_rate_aave()
        task_to_db(os.path.basename(__file__), 'update_margin_interest_rate_aave', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_margin_interest_rate_aave", message=msg)
        #raise e

    t9 = time.time()
    # 更新舆情数据
    task_to_db(os.path.basename(__file__), 'update_social_data_aicoin_bulletin')
    try:
        update_social_data_aicoin_bulletin()
        task_to_db(os.path.basename(__file__), 'update_social_data_aicoin_bulletin', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_social_data_aicoin_bulletin", message=msg)
        #raise e

    task_to_db(os.path.basename(__file__), 'update_social_metrics_btc')
    try:
        update_social_metrics_btc()
        task_to_db(os.path.basename(__file__), 'update_social_metrics_btc', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_social_metrics_btc", message=msg)
        #raise e

    t10 = time.time()
    # 更新glassnode因子
    task_to_db(os.path.basename(__file__), 'update_all_indicators')
    try:
        update_all_indicators()
        task_to_db(os.path.basename(__file__), 'update_all_indicators', 1)
    except Exception as e:
        msg = traceback.format_exc()
        logger.info(msg)
        send_error_to_email(script_name=os.path.basename(__file__), func_name="update_all_indicators", message=msg)
        #raise e

    t11 = time.time()
    logger.info('各任务用时')
    logger.info(f'币的信息和板块信息:{t2 - t1}')
    logger.info(f'行情数据:{t3 - t2}')
    logger.info(f'deribit期权交易数据:{t4 - t3}')
    logger.info(f'交易所信息:{t5 - t4}')
    logger.info(f'交易所交易量信息:{t6 - t5}')
    logger.info(f'USDT兑人民币相关:{t7 - t6}')
    logger.info(f'币安资金费率:{t8 - t7}')
    logger.info(f'交易所借贷利率:{t9 - t8}')
    logger.info(f'舆情数据:{t10 - t9}')
    logger.info(f'glassnode因子:{t11 - t10}')
    time.sleep(3600)
